export const TYPES = {
  GET_REWARD_LIST: "reward/getrewardlist",
  GET_REWARD_LIST_SUCCESS: "reward/getrewardlistsuccess",
  GET_REWARD_LIST_FAIL: "reward/getrewardlistfail",
  SAVE_REWARD: "reward/savereward",
  SAVE_REWARD_SUCCESS: "reward/saverewardsuccess",
  SAVE_REWARD_FAIL: "reward/saverewardfail",
  DELETE_REWARD: "reward/deletereward",
  DELETE_REWARD_SUCCESS: "reward/deleterewardsuccess",
  DELETE_REWARD_FAIL: "reward/deleterewardfail",
  GET_REWARD_PRODUCTS: "reward/getrewardproducts",
  GET_REWARD_PRODUCTS_SUCCESS: "reward/getrewardproductssuccess",
  GET_REWARD_PRODUCTS_FAIL: "reward/getrewardproductsfail",
  EDIT_EXISTING_REWARD: "reward/editexistingreward",
  ADD_NEW_REWARD: "reward/addnewreward",
  CANCEL_EDIT_REWARD: "reward/canceleditreward",
};

const EmptyReward = {
  product_id: '',
  status: 0,
  sort_order: 0,
  date_expired: '',
  title: '',
  take_points: 0,
  description: '',
  image: '',
};

const DefaultStore = {
  loading: false,
  saving: false,
  error: false,
  editingReward: {},
  products: [],
  rewards: [],
};

export default function(store = DefaultStore, action) {
  switch (action.type) {
    case TYPES.GET_REWARD_LIST:
      return {
        ...store,
        loading: true,
        error: false,
      };

    case TYPES.GET_REWARD_LIST_SUCCESS:
      return {
        ...store,
        loading: false,
        error: false,
        rewards: action.rewards,
      };

    case TYPES.GET_REWARD_LIST_FAIL:
      return {
        ...store,
        loading: false,
        rewards: [],
        error: true,
      };

    case TYPES.SAVE_REWARD:
      return {
        ...store,
        saving: true,
        error: false,
      };

    case TYPES.SAVE_REWARD_SUCCESS:
      const rewards = (action.is_new) ?
        [...store.rewards, action.reward] :
        store.rewards.map(item => {
          if (item.server_reward_id === action.reward.reward_id) {
            return { ...item, ...action.reward };
          }

          return item;
        });

      return {
        ...store,
        saving: false,
        error: false,
        rewards: rewards,
        editingReward: {},
      };

    case TYPES.SAVE_REWARD_FAIL:
      return {
        ...store,
        saving: false,
        error: true,
      };

    case TYPES.DELETE_REWARD:
      return {
        ...store,
        loading: true,
        error: false,
      };

    case TYPES.DELETE_REWARD_SUCCESS:
      return {
        ...store,
        loading: false,
        error: false,
        rewards: store.rewards.filter(reward => reward.server_reward_id != action.reward_id),
        editingReward: {},
      };

    case TYPES.DELETE_REWARD_FAIL:
      return {
        ...store,
        loading: false,
        error: true,
      };

    case TYPES.GET_REWARD_PRODUCTS:
      return {
        ...store,
      };

    case TYPES.GET_REWARD_PRODUCTS_SUCCESS:
      return {
        ...store,
        products: action.products,
      };

    case TYPES.GET_REWARD_PRODUCTS_FAIL:
      return {
        ...store,
        products: [],
      };

    case TYPES.ADD_NEW_REWARD:
      return {
        ...store,
        editingReward: EmptyReward,
      };

    case TYPES.EDIT_EXISTING_REWARD:
      return {
        ...store,
        editingReward: action.reward,
      };

    case TYPES.CANCEL_EDIT_REWARD:
      return {
        ...store,
        editingReward: {},
      };

    default:
      return store;
  }
}